<?php

namespace App\Http\Controllers\BG;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;

use \GatewayWorker\Lib\Gateway;

use Illuminate\Support\Facades\DB;
use Cache;
use Validator;
use Carbon\Carbon;

use  App\Models\SMS;
use  App\Models\User;
use  App\Models\Phone;
use  App\Models\ChatMessage;
use  App\Models\Weixin;
use  App\Models\WeixinFriendList;
class IndexController extends Controller
{
    public function index(Request $request)
    {
        $input = Input::only('start_at','end_at');
        $validator = Validator::make($input, [        
            'start_at' => 'nullable',
            'end_at' => 'nullable',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $start_at =  $input['start_at']??Carbon::now()->subDay(7);
        $end_at =  $input['end_at']??Carbon::now();
        $data['all'] = $this->getAllData($start_at,$end_at);
        $data['new_friend'] = $this->getNewFriendData($start_at,$end_at);
        $data['statics_friend'] = $this->getStaticsFriendData($start_at,$end_at);
        $data['statics_chat'] = $this->getStaticsChatData($start_at,$end_at);
        // $data['friend_growth'] = $this->getFriendGrowth($start_at,$end_at);
        // $data['user'] = $this->getUser($start_at,$end_at);
        return $this->returnOk($data);
    }

    protected function getAllData($start_at,$end_at)
    {
        $data['phone'] = Phone::where('created_at','>',$start_at)->where('created_at','<',$end_at)->count();
        $data['weixin'] = Weixin::where('created_at','>',$start_at)->where('created_at','<',$end_at)->count();
        $data['send'] = ChatMessage::where('created_at','>',$start_at)->where('created_at','<',$end_at)->where('is_send',1)->count();
        $data['receive'] = ChatMessage::where('created_at','>',$start_at)->where('created_at','<',$end_at)->where('is_send',0)->count();
        return $data;
    }

    protected function getNewFriendData($start_at,$end_at)
    {
        $data['all'] = WeixinFriendList::where('created_at','>',$start_at)->where('created_at','<',$end_at)->count();
        $data['man'] = WeixinFriendList::where('created_at','>',$start_at)->where('created_at','<',$end_at)->where('sex',1)->count();
        $data['women'] = WeixinFriendList::where('created_at','>',$start_at)->where('created_at','<',$end_at)->where('sex',2)->count();
        $data['unknown'] = WeixinFriendList::where('created_at','>',$start_at)->where('created_at','<',$end_at)->where('sex',0)->count();
        return $data;
    }

    protected function getStaticsFriendData($start_at,$end_at)
    {
        $data['all'] = WeixinFriendList::where('created_at','>',$start_at)->where('created_at','<',$end_at)->count();
        $data['man'] = WeixinFriendList::where('created_at','>',$start_at)->where('created_at','<',$end_at)->where('sex',1)->count();
        $data['women'] = WeixinFriendList::where('created_at','>',$start_at)->where('created_at','<',$end_at)->where('sex',2)->count();
        $data['unknown'] = WeixinFriendList::where('created_at','>',$start_at)->where('created_at','<',$end_at)->where('sex',0)->count();
        return $data;
    }

    protected function getStaticsChatData($start_at,$end_at)
    {
        $data['all'] = ChatMessage::leftJoin('t_weixin_friend_list','t_weixin_friend_list.uid', '=', 't_chat_message.to_weixin_uid')
                ->where('t_chat_message.created_at','>',$start_at)->where('t_chat_message.created_at','<',$end_at)
                ->count();
        $data['man'] = ChatMessage::leftJoin('t_weixin_friend_list','t_weixin_friend_list.uid', '=', 't_chat_message.to_weixin_uid')
                ->where('t_chat_message.created_at','>',$start_at)->where('t_chat_message.created_at','<',$end_at)
                ->where('t_weixin_friend_list.sex',1)->count();
        $data['women'] = ChatMessage::leftJoin('t_weixin_friend_list','t_weixin_friend_list.uid', '=', 't_chat_message.to_weixin_uid')
                ->where('t_chat_message.created_at','>',$start_at)->where('t_chat_message.created_at','<',$end_at)
                ->where('t_weixin_friend_list.sex',2)->count();
        $data['unknown'] = ChatMessage::leftJoin('t_weixin_friend_list','t_weixin_friend_list.uid', '=', 't_chat_message.to_weixin_uid')
                ->where('t_chat_message.created_at','>',$start_at)->where('t_chat_message.created_at','<',$end_at)
                ->where('t_weixin_friend_list.sex',0)->count();
        return $data;

    }

    public function friend()
    {
        $input = Input::only('page','limit','start_at','end_at');
        $validator = Validator::make($input, [        
            'start_at' => 'nullable',
            'end_at' => 'nullable',
            'page' => 'required',
            'limit' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $start_at =  $input['start_at']??Carbon::now()->subDay(7);
        $end_at =  $input['end_at']??Carbon::now();
        $page = $input['page'];
        $limit = $input['limit'];
        $user = Weixin::leftJoin('t_weixin_friend_list','t_weixin_friend_list.weixin_uid', '=', 't_weixin.uid')
            ->where('t_weixin_friend_list.created_at','>',$start_at)->where('t_weixin_friend_list.created_at','<',$end_at)
            ->select('t_weixin.uid','t_weixin.nickname','t_weixin.alias','t_weixin.small_head_url')
            ->selectRaw('count(t_weixin_friend_list.id) as allCount')
            ->groupBy('t_weixin.uid','t_weixin.nickname','t_weixin.alias','t_weixin.small_head_url')
            ->orderBy('allCount', 'desc')
            ->offset(($page-1)*$limit)
            ->limit($limit)
            ->get();
        foreach ($user as $key => $value) {
            $user[$key]['all'] = $value['allCount'];
            $user[$key]['man'] = WeixinFriendList::where('created_at','>',$start_at)->where('user_id',$value['id'])->where('created_at','<',$end_at)->where('sex',1)->count();
            $user[$key]['women'] = WeixinFriendList::where('created_at','>',$start_at)->where('user_id',$value['id'])->where('created_at','<',$end_at)->where('sex',2)->count();
            $user[$key]['unknown'] = WeixinFriendList::where('created_at','>',$start_at)->where('user_id',$value['id'])->where('created_at','<',$end_at)->where('sex',0)->count();
        }
        $count  = Weixin::count();
        $fin['count'] = $count;
        $fin['data'] = $user;
        return $this->returnOk($fin);
    }

    public function user()
    {
        $input = Input::only('page','limit','start_at','end_at');
        $validator = Validator::make($input, [        
            'start_at' => 'nullable',
            'end_at' => 'nullable',
            'page' => 'required',
            'limit' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->returnError([],401,'参数错误');
        }
        $start_at =  $input['start_at']??Carbon::now()->subDay(7);
        $end_at =  $input['end_at']??Carbon::now();
        $page = $input['page'];
        $limit = $input['limit'];
        $user = User::offset(($page-1)*$limit)
            ->limit($limit)
            ->get();
        $count  = User::count();
        $fin_data = array();
        foreach ($user as $key => $value) {
            $temp['name'] = $value['name'];
            $temp['all'] = WeixinFriendList::where('created_at','>',$start_at)->where('user_id',$value['id'])->where('created_at','<',$end_at)->count();
            $temp['man'] = WeixinFriendList::where('created_at','>',$start_at)->where('user_id',$value['id'])->where('created_at','<',$end_at)->where('sex',1)->count();
            $temp['women'] = WeixinFriendList::where('created_at','>',$start_at)->where('user_id',$value['id'])->where('created_at','<',$end_at)->where('sex',2)->count();
            $temp['unknown'] = WeixinFriendList::where('created_at','>',$start_at)->where('user_id',$value['id'])->where('created_at','<',$end_at)->where('sex',0)->count();
            $fin_data[] = $temp;
        }
        $fin['count'] = $count;
        $fin['data'] = $fin_data;
        return $this->returnOk($fin);

    }
}